//
//  CZHomeTableViewController.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let homeID = "HomeTableViewCellIdentifier"
class CZHomeTableViewController: CZVisitorTableViewController {
//	var statusList: [CZStatus]?
//	var statusList: CZStatusViewModels?
	//上啦加载视图
	private lazy var pulllUpView: UIActivityIndicatorView = {
	let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
	view.color = UIColor.redColor()
		return view
	}()
	//系统下拉刷新
	private lazy var refreshCtr: UIRefreshControl = {
	let ctr = UIRefreshControl()
	
		return ctr
	}()
	//自定义的下拉刷新
	private lazy var pullDownView: CZtestRefresContro = {
		let ctr = CZtestRefresContro()
		return ctr
	}()
	//tip 动画的空间
	private lazy var tipLabel: UILabel = {
	let label = UILabel(textColor: UIColor.whiteColor(), fontSIze: 12)
	label.textAlignment = .Center
		label.backgroundColor = UIColor.orangeColor()
		label.hidden = true
	return label
	}()
	
    private lazy var statusListViewModel: CZStatusListViewModel = CZStatusListViewModel()
	
//	private lazt var statusLisrViewModel:
    override func viewDidLoad() {
        super.viewDidLoad()

        if userLogin{
			//添加空间约束
			setUpUI()
			//设置 tab 的相关操作
        setupTab()
//		requestStatus()
			loadStatus()
//			let ctr = CZtestRefresContro()
//			ctr.addTarget(self, action: "testRefressh:", forControlEvents: .ValueChanged)
//			tableView.addSubview(ctr)
        }else
        {
//未登录情况
                vistourView?.setVistorInfo(nil, imageName: nil)
        
        }
    }
	
	private func setUpUI() {
		if let nav = self.navigationController {
			tipLabel.frame = CGRectMake(0, CGRectGetMaxY(nav.navigationBar.frame) - 35, tableView.frame.size.width, 35)
			 nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
		}
		
	
	
	}
	
	private func loadStatus() {
		statusListViewModel.requestStatus(pulllUpView.isAnimating(), complete: { (isSuccess, message) -> () in
		
			if !self.pulllUpView.isAnimating() {
			//执行动画
				self.startAnimation(message)
			
			}
			//闭包加 self
			self.endrefreshing()
			if isSuccess {
				self.tableView.reloadData()
			}
		})

	
	}
	
	private func endrefreshing() {
		//
		pulllUpView.stopAnimating()
		refreshCtr.endRefreshing()
		//自定义空间结束刷新
		pullDownView.endRefesh()
	}
	

	
	private func setupTab() {
	
	tableView.registerClass(CZStatusTableViewCell.self, forCellReuseIdentifier: homeID)
		//设置行高
		//自动计算行高
		tableView.rowHeight = UITableViewAutomaticDimension
		//预估高度
		tableView.estimatedRowHeight = 200;
		tableView.separatorStyle = .None
		tableView.tableFooterView = pulllUpView
		//设置大小,防止显示出来没有留位置
		pulllUpView.sizeToFit()
	//添加下拉刷新的第一种方式
//		refreshCtr.addTarget(self, action: #selector(CZHomeTableViewController.pullDownRefesh), forControlEvents: .ValueChanged)
//		self.refreshControl = refreshCtr
		
		pullDownView.addTarget(self, action: #selector(CZHomeTableViewController.pullDownRefesh), forControlEvents: .ValueChanged)
		
		//添加自定义下拉刷新
		tableView.addSubview(pullDownView)
	}
	
	private func startAnimation(message: String) {
		//防止动画持续执行
		if tipLabel.hidden == false {
			return
		}
		
		//赋值显示内容
		tipLabel.text = message
		tipLabel.hidden = false
		
		UIView.animateWithDuration(1, animations: { () -> Void in
			self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.frame.size.height)
		}) { (_) -> Void in
			
			UIView.animateWithDuration(1, animations: { () -> Void in
				//  回到原始位置
				self.tipLabel.transform = CGAffineTransformIdentity
				
				}, completion: { (_) -> Void in
					self.tipLabel.hidden = true
			})
			
		}
	}
	
	@objc private func pullDownRefesh() {
		
		//下拉刷新加载数据
		loadStatus()
//		dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () in
//		//结束刷新
//			self.refreshCtr.endRefreshing()
//
//		})
	
	}
	// MARK: ------测试刷新------
	func testRefressh(ctr: CZtestRefresContro) {
	
				dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () in
				//结束刷新
//					self.refreshCtr.endRefreshing()
			ctr.endRefesh()
				})
	
	
	}
}
extension CZHomeTableViewController {
	override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//		return statusList?.count ?? 0
	
	return statusListViewModel.statusList?.count ?? 0
	}
	
	//将要显示的 cell
	override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
		if indexPath.row == statusListViewModel.statusList!.count - 1 && !pulllUpView.isAnimating() {
			
			
			//开启动画
			pulllUpView.startAnimating()
			loadStatus()
//			print("last")
			//数据请求
		}
	}
	
	override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier(homeID, forIndexPath: indexPath) as! CZStatusTableViewCell
		cell.statusViewModel = statusListViewModel.statusList![indexPath.row]
		//去掉选中颜色
		cell.selectionStyle = .None
//		cell.textLabel?.text = statusList![indexPath.row].user?.screen_name
		return cell
		
	}
	
	
	
	
	
	
	
	
	}
	
	